//
//  LBUserGuideView.m
//  FBSnapshotTestCase
//
//  Created by LINAICAI on 2017/10/31.
//

#import "LBUserGuideView.h"
#import <Masonry/Masonry.h>

@implementation LBUserGuideView
- (instancetype)initWithImage:(UIImage *)image
                 imagePositon:(LBUserGuideImagePosition)imagePositon
                     maskView:(UIView *)maskView
               maskViewMargin:(CGFloat)maskViewMargin
               maskViewCorner:(CGFloat)maskViewCorner
                     afterTap:(void(^)(void))afterTap
{
    self = [super initWithFrame:CGRectZero];
    if (self) {
        _image = image;
        _imagePositon = imagePositon;
        _maskView = maskView;
        _maskViewCorner = maskViewCorner;
        _maskViewMargin = maskViewMargin;
        _afterTap = afterTap;
    }
    return self;
}
- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc]init];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _imageView;
}
/**
 显示出来

 @param view 显示在哪个视图上面
 */
- (void)showInView:(UIView *)view{
    
    //如果没有包含本视图，才add进去
    if (![view.subviews containsObject:self]) {
        [view addSubview:self];
        
        self.frame = view.bounds;
        self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.8];
        [self addTarget:self action:@selector(tapView:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.imageView];
    }
    //设置图片
    self.imageView.image = self.image;
    CGRect rect = [_maskView.superview convertRect:_maskView.frame toView:view];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
    [path appendPath:[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:_maskViewCorner] bezierPathByReversingPath]];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path =  path.CGPath;
    shapeLayer.backgroundColor = [UIColor grayColor].CGColor;
    shapeLayer.fillColor = [UIColor grayColor].CGColor;
    shapeLayer.strokeColor = [UIColor grayColor].CGColor;
    shapeLayer.lineWidth = 1/[UIScreen mainScreen].scale;
    
    [self.layer setMask:shapeLayer];
    
    if (_imagePositon == LBUserGuideImageInTop) {
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_maskView.mas_top).mas_offset(-_maskViewMargin);
            make.width.equalTo(self);
            
        }];
    }
    else if (_imagePositon == LBUserGuideImageInBottom) {
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_maskView.mas_bottom).mas_offset(_maskViewMargin);
            make.width.equalTo(self);
            
        }];

    }
    
    __weak typeof(self) weakSelf = self;
    //显示视图的动画
    [UIView animateWithDuration:0.5 animations:^{
        weakSelf.alpha = 1.f;
    } completion:^(BOOL finished) {
        
    }];
}
- (void)tapView:(id)sender{
    [self hid];
}
- (void)hid{
    __weak typeof(self) weakSelf = self;
    self.alpha = 0.f;
    if (self.afterTap) {
        self.afterTap();
        [weakSelf removeFromSuperview];
    }
    else{
        [weakSelf removeFromSuperview];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
